#include "..\\include\Lib_Clara.h"
#include "..\\include\Dir.h"
#include "GUI.h"
#include "snapshot.h"
//extern unsigned char Snapshot (const wchar_t *filename);
//extern unsigned char S9xLoadSnapshot (const wchar_t *filename);
extern wchar_t game_filename[];
extern int SettingsOnKey();
extern int set_pause;
extern int running;
extern void S9xReset (void);

void snap_fname(wchar_t *fname, wchar_t *snapname, wchar_t no)
{    
  wstrcpy (snapname, fname);
  wchar_t *s=wstrrchr(snapname,'.');
  *++s=L'0'; *++s=L'0'; *++s=no;
}


int SaveHook(int key, int r1 , int mode) 
{    
 wchar_t save_snap[256];
if (mode==KBD_SHORT_PRESS)
{
  if (key==KEY_DIGITAL_0)
  {
     snap_fname(game_filename, save_snap, '0');
     S9xFreezeGame (save_snap);     
  }  
  if (key==KEY_DIGITAL_0+1)
  {
     snap_fname(game_filename, save_snap, '1');
     S9xFreezeGame (save_snap);     
  }
  if (key==KEY_DIGITAL_0+2)
  { 
     snap_fname(game_filename, save_snap, '2');    
     S9xFreezeGame (save_snap);
  }
  if (key==KEY_DIGITAL_0+3)
  {  
     snap_fname(game_filename, save_snap, '3');
     S9xFreezeGame (save_snap);
  }
  if (key==KEY_DIGITAL_0+4)
  {    
     snap_fname(game_filename, save_snap, '4');
     S9xFreezeGame (save_snap);
  }
  if (key==KEY_DIGITAL_0+5)
  {    
     snap_fname(game_filename, save_snap, '5');
     S9xFreezeGame (save_snap);
  }
  if (key==KEY_DIGITAL_0+6)
  {
     snap_fname(game_filename, save_snap, '6');
     S9xFreezeGame (save_snap);     
  }
  if (key==KEY_DIGITAL_0+7)
  {
     snap_fname(game_filename, save_snap, '7');
     S9xFreezeGame (save_snap);     
  }
  if (key==KEY_DIGITAL_0+8)
  {
     snap_fname(game_filename, save_snap, '8');
     S9xFreezeGame (save_snap);     
  }
  if (key==KEY_DIGITAL_0+9)
  {
     snap_fname(game_filename, save_snap, '9');
     S9xFreezeGame (save_snap);     
  }
  if (key==KEY_LEFT_SOFT || key==KEY_RIGHT_SOFT || key==KEY_ENTER)
  {
    ModifyKeyHook(SaveHook,0);     
  }
}
return 0;
}

int LoadHook(int key, int r1 , int mode) 
{    
  wchar_t save_snap[256];
 if (mode==KBD_SHORT_PRESS)
 {
  if (key==KEY_DIGITAL_0)
  {  
    snap_fname(game_filename, save_snap, '0');      
    S9xUnfreezeGame (save_snap);       
  }
  if (key==KEY_DIGITAL_0+1)
  {  
    snap_fname(game_filename, save_snap, '1');      
    S9xUnfreezeGame (save_snap);       
  }
  if (key==KEY_DIGITAL_0+2)
  {  
    snap_fname(game_filename, save_snap, '2'); 
    S9xUnfreezeGame (save_snap);
  }
  if (key==KEY_DIGITAL_0+3)
  { 
    snap_fname(game_filename, save_snap, '3');
    S9xUnfreezeGame (save_snap);
  }
  if (key==KEY_DIGITAL_0+4)
  {  
    snap_fname(game_filename, save_snap, '4');
    S9xUnfreezeGame (save_snap);
  }  
  if (key==KEY_DIGITAL_0+5)
  {  
    snap_fname(game_filename, save_snap, '5');
    S9xUnfreezeGame (save_snap);
  }
  if (key==KEY_DIGITAL_0+6)
  {  
    snap_fname(game_filename, save_snap, '6');
    S9xUnfreezeGame (save_snap);
  }
   if (key==KEY_DIGITAL_0+7)
  {  
    snap_fname(game_filename, save_snap, '7');
    S9xUnfreezeGame (save_snap);
  }
   if (key==KEY_DIGITAL_0+8)
  {  
    snap_fname(game_filename, save_snap, '8');
    S9xUnfreezeGame (save_snap);
  }
   if (key==KEY_DIGITAL_0+9)
  {  
    snap_fname(game_filename, save_snap, '9');
    S9xUnfreezeGame (save_snap);
  }
  if (key==KEY_LEFT_SOFT || key==KEY_RIGHT_SOFT || key==KEY_ENTER)
  {
    ModifyKeyHook(LoadHook,0);     
  } 
 }
return 0;
}



void GuiOnEnterPressed(BOOK *bk, GUI *lt)
{    
  int item=ListMenu_GetSelectedItem(MyBook->stgui);
  if(item==0) 
  {      
    //memorydump();
    GUIObject_Destroy(MyBook->stgui);
    set_pause=0;    
  }
  if(item==1) 
  {
  MessageBox (0x6fFFFFFF, STR("Select state to save(0-9)\n OK to return") , 0 , 1 , 0, 0);      
  ModifyKeyHook(SaveHook,1);    
  }
  if(item==2)
  {
   MessageBox (0x6fFFFFFF, STR("Select state to load(0-9)\n OK to return") , 0 , 1 , 0, 0); 
   ModifyKeyHook(LoadHook,1);
  }
  if(item==3) 
    SettingsOnKey();
  if(item==4) 
    MessageBox(0x6fffffff,STR("SEns9x v0.70 2009-2010 by CentrumSE.pl developers: MaW, mc_kibel, Marian90\nThanks to Tartes!\n \n \n Siemens elf by cbn, Miha_r"),0,1,0,0);
  if(item==5) 
  {
    GUIObject_Destroy(MyBook->stgui);
    set_pause=0;  
    S9xReset ();
  }    
  if(item==6){
    GUIObject_Destroy( MyBook->stgui);
    running=0;   
    return;
  }
}

int GuiOnLBMessage(GUI_MESSAGE * msg)
{
  int item;  
  switch(GUIonMessage_GetMsg(msg))
  {    
  case 1:
    item=GUIonMessage_GetCreatedItemIndex(msg);    
    if(item==0)    
      GUIonMessage_SetMenuItemText(msg,Str2ID(L"Resume",0,SID_ANY_LEN));
    if(item==1)    
      GUIonMessage_SetMenuItemText(msg,Str2ID(L"Save",0,SID_ANY_LEN)); 
    if(item==2)    
      GUIonMessage_SetMenuItemText(msg,Str2ID(L"Load",0,SID_ANY_LEN)); 
    if(item==3)
      GUIonMessage_SetMenuItemText(msg,Str2ID(L"Settings",0,SID_ANY_LEN)); 
    if(item==4)
      GUIonMessage_SetMenuItemText(msg,Str2ID(L"About",0,SID_ANY_LEN));
    if(item==5)
      GUIonMessage_SetMenuItemText(msg,Str2ID(L"Reset",0,SID_ANY_LEN));
    if(item==6)
      GUIonMessage_SetMenuItemText(msg,Str2ID(L"Exit",0,SID_ANY_LEN));
  }
  return(1);
};

void GuiCreateGuiList(void * r0, BOOK * bk, wchar_t * GUI_NAME, int ItemNumber)
{    
  MyBook->stgui=CreateListMenu(bk,0);
  GUIObject_SetTitleText(MyBook->stgui,Str2ID(GUI_NAME,0,SID_ANY_LEN));
  ListMenu_SetItemCount(MyBook->stgui,ItemNumber);
  OneOfMany_SetOnMessage((GUI_ONEOFMANY*)MyBook->stgui,GuiOnLBMessage);
  ListMenu_SetCursorToItem(MyBook->stgui,0);
  ListMenu_SetItemStyle(MyBook->stgui,0); 
  GUIObject_SoftKeys_SetAction(MyBook->stgui,ACTION_SELECT1,GuiOnEnterPressed);     
  GUIObject_Show(MyBook->stgui);  
};

